{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
   "metadata": {},
   "source": [
    "Chapter 06\n",
    "\n",
    "# 秩\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
   "metadata": {},
   "source": [
    "这段代码的核心目的是利用线性代数的方法，分析不同矩阵的秩（rank），即它们的列向量是否线性相关或独立。代码主要通过构造不同的列向量矩阵，然后计算其秩，来验证线性代数中的基本概念。\n",
    "\n",
    "---\n",
    "\n",
    "### **初始化标准基向量**\n",
    "代码首先定义了标准基向量 $e_1, e_2, e_3$，这些向量是 $\\mathbb{R}^3$ 空间中的单位列向量：\n",
    "$$\n",
    "e_1 = \\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\end{bmatrix}, \\quad\n",
    "e_2 = \\begin{bmatrix} 0 \\\\ 1 \\\\ 0 \\end{bmatrix}, \\quad\n",
    "e_3 = \\begin{bmatrix} 0 \\\\ 0 \\\\ 1 \\end{bmatrix}\n",
    "$$\n",
    "这些向量构成了 $\\mathbb{R}^3$ 的标准正交基。\n",
    "\n",
    "---\n",
    "\n",
    "### **计算矩阵 $A$ 的秩**\n",
    "矩阵 $A$ 由 $e_1, e_2, e_3$ 作为列向量构成：\n",
    "$$\n",
    "A = \\begin{bmatrix} e_1 & e_2 & e_3 \\end{bmatrix} =\n",
    "\\begin{bmatrix}\n",
    "1 & 0 & 0 \\\\\n",
    "0 & 1 & 0 \\\\\n",
    "0 & 0 & 1\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "这是 $3 \\times 3$ 的单位矩阵（即标准基向量组成的矩阵），它的列向量是线性无关的，因此矩阵 $A$ 的秩为：\n",
    "$$\n",
    "\\text{rank}(A) = 3\n",
    "$$\n",
    "\n",
    "---\n",
    "\n",
    "### **计算矩阵 $B$ 的秩**\n",
    "矩阵 $B$ 由 $e_1, e_2, e_1 + e_2$ 作为列向量构成：\n",
    "$$\n",
    "B = \\begin{bmatrix} e_1 & e_2 & e_1 + e_2 \\end{bmatrix} =\n",
    "\\begin{bmatrix}\n",
    "1 & 0 & 1+0 \\\\\n",
    "0 & 1 & 0+1 \\\\\n",
    "0 & 0 & 0+0\n",
    "\\end{bmatrix}\n",
    "=\n",
    "\\begin{bmatrix}\n",
    "1 & 0 & 1 \\\\\n",
    "0 & 1 & 1 \\\\\n",
    "0 & 0 & 0\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "这个矩阵的第三列是前两列的线性组合：\n",
    "$$\n",
    "e_1 + e_2 = 1 \\cdot e_1 + 1 \\cdot e_2\n",
    "$$\n",
    "因此，矩阵 $B$ 的秩为 $2$，即：\n",
    "$$\n",
    "\\text{rank}(B) = 2\n",
    "$$\n",
    "因为它只有两个线性无关的列向量。\n",
    "\n",
    "---\n",
    "\n",
    "### **计算矩阵 $C$ 的秩**\n",
    "矩阵 $C$ 由 $e_1, e_2$ 作为列向量构成：\n",
    "$$\n",
    "C = \\begin{bmatrix} e_1 & e_2 \\end{bmatrix} =\n",
    "\\begin{bmatrix}\n",
    "1 & 0 \\\\\n",
    "0 & 1 \\\\\n",
    "0 & 0\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "该矩阵只有两列，它们是线性无关的，因此秩为：\n",
    "$$\n",
    "\\text{rank}(C) = 2\n",
    "$$\n",
    "\n",
    "---\n",
    "\n",
    "### **计算矩阵 $D$ 的秩**\n",
    "矩阵 $D$ 由 $e_1, -e_1, \\frac{1}{2} e_1$ 作为列向量构成：\n",
    "$$\n",
    "D = \\begin{bmatrix} e_1 & -e_1 & \\frac{1}{2} e_1 \\end{bmatrix} =\n",
    "\\begin{bmatrix}\n",
    "1 & -1 & \\frac{1}{2} \\\\\n",
    "0 & 0 & 0 \\\\\n",
    "0 & 0 & 0\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "由于第二、第三行全为零，该矩阵的秩仅由第一行决定。观察其列向量：\n",
    "- 第二列是第一列的 $-1$ 倍。\n",
    "- 第三列是第一列的 $\\frac{1}{2}$ 倍。\n",
    "\n",
    "因此，矩阵 $D$ 只有一个线性无关的列向量，它的秩为：\n",
    "$$\n",
    "\\text{rank}(D) = 1\n",
    "$$\n",
    "\n",
    "---\n",
    "\n",
    "### **总结**\n",
    "- $A$ 是单位矩阵，列向量线性无关，$\\text{rank}(A) = 3$。\n",
    "- $B$ 中的第三列是前两列的线性组合，$\\text{rank}(B) = 2$。\n",
    "- $C$ 只有两个独立的列向量，$\\text{rank}(C) = 2$。\n",
    "- $D$ 的三列是线性相关的，秩降为 $1$，即 $\\text{rank}(D) = 1$。\n",
    "\n",
    "通过这四个案例，我们验证了线性代数中的基本秩概念，包括：\n",
    "1. 单位矩阵的秩等于维数。\n",
    "2. 向量线性相关时会导致秩降低。\n",
    "3. 线性组合不会增加秩。\n",
    "\n",
    "这一分析在高维空间、特征分析、降维（如 PCA）等问题中有广泛应用。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36c972a8-94e6-4b88-9f47-5adb545d316c",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2018861a-2d40-43b7-88af-820b495e1b17",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8d9872d-d32a-4600-853b-c8879f2f91d9",
   "metadata": {},
   "source": [
    "## 定义列向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fed51ffe-8a9a-4b79-9d40-842f57aca417",
   "metadata": {},
   "outputs": [],
   "source": [
    "e_1 = np.array([[1],\n",
    "                [0],\n",
    "                [0]])\n",
    "\n",
    "e_2 = np.array([[0],\n",
    "                [1],\n",
    "                [0]])\n",
    "\n",
    "e_3 = np.array([[0],\n",
    "                [0],\n",
    "                [1]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c59e7d87-37b1-4682-bbfd-35a17e0c5f82",
   "metadata": {},
   "source": [
    "## 计算秩"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "85ac6fc6-35e6-4f89-a3ed-04b568716fef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 0],\n",
       "       [0, 1, 0],\n",
       "       [0, 0, 1]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.column_stack([e_1, e_2, e_3])\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "17c07eb4-7483-434f-a9e4-b126ae212ef5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.matrix_rank(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9712dd7c-81f7-4761-a364-0d1b91c484a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 1],\n",
       "       [0, 1, 1],\n",
       "       [0, 0, 0]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = np.column_stack([e_1, e_2, e_1 + e_2])\n",
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e7d35475-1ae5-43b4-a05c-464fc6682e1e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.matrix_rank(B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "416c5357-be84-4984-912c-82e41d8418f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0],\n",
       "       [0, 1],\n",
       "       [0, 0]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C = np.column_stack([e_1, e_2])\n",
    "C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "7e119958-08c7-4e90-8c63-127900abf6d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.matrix_rank(C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d3d777a3-dacf-4fa0-a40b-b4a4ca164154",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1. , -1. ,  0.5],\n",
       "       [ 0. ,  0. ,  0. ],\n",
       "       [ 0. ,  0. ,  0. ]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D = np.column_stack([e_1, -e_1, e_1/2])\n",
    "D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "84beafae-29d1-4c0e-bc97-c2f3bb6612e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.matrix_rank(D)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "070c3389-8048-43a3-baa7-6666009bce96",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
